package controler;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import castleContent.User;
import castleContent.Users;
import dataBase.CheckLoginWithDBData;
import dataBase.DataBase;
import dataBase.RetrieveDBData;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int gold = 0, food = 0,  goldProduction = 0, foodProduction = 0;
		int armyCapacity = 0 ,barbarians = 0, archers = 0, wizards = 0;
		int barbarianAd = 0, barbarianHp = 0, archerAd = 0, archerHp = 0, wizardAd = 0, wizardHp = 0;
		try {
			gold = RetrieveDBData.getGoldFromCurrentLoggedInUser(
					DataBase.getDbCon(), username);
			food = RetrieveDBData.getFoodFromCurrentLoggedInUser(
					DataBase.getDbCon(), username);
			armyCapacity = RetrieveDBData
					.getArmy_capacityFromCurrentLoggedInUser(
							DataBase.getDbCon(), username);
			barbarians = RetrieveDBData.getBarbariansFromCurrentLoggedInUser(DataBase.getDbCon(), username);
			
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		try {
			if (isValidUsernameAndPassword(username, password)) {
				response.sendRedirect("InvalidLogin.html");
			} else if (isRegisteredUser(username, password)) {
				session.setAttribute("User", username);

				if (!Users.users.containsKey(username)) {
					User user = new User(username, password, gold,
							goldProduction, foodProduction, food, armyCapacity,
							archers, barbarians, wizards, barbarianAd,
							barbarianHp, archerAd, archerHp, wizardAd,
							wizardHp);
					Users.users.put(username, user);
					session.setAttribute("Castle", user.castle);
					session.setAttribute("Object", user);
				} else {
					User user = new User(username, password, gold,
							goldProduction, foodProduction, food, armyCapacity,
							archers, barbarians, wizards, barbarianAd,
							barbarianHp, archerAd, archerHp, wizardAd,
							wizardHp);
					session.setAttribute("Castle", user.castle);
					session.setAttribute("Object", user);
				}
				response.sendRedirect("HumanCastle.jsp");
			}
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
	}

	private boolean isRegisteredUser(String username, String password)
			throws ClassNotFoundException, SQLException {
		return CheckLoginWithDBData.checkIfUsernameExistsInDB(username)
				&& CheckLoginWithDBData.checkIfPasswordExistsInDB(password);
	}

	private boolean isValidUsernameAndPassword(String username, String password)
			throws ClassNotFoundException, SQLException {
		return CheckLoginWithDBData.checkIfUsernameExistsInDB(username) == false
				|| CheckLoginWithDBData.checkIfPasswordExistsInDB(password) == false
				|| CheckLoginWithDBData.checkIfIndexesOnBothListsAreTheSame(
						username, password) == false;
	}
}
